home *** CD-ROM | disk | FTP | other *** search
/ Magnum One / Magnum One (Mid-American Digital) (Disc Manufacturing).iso / d18 / tprolog.arc / PREDS.PRO < prev    next >
Text File  |  1991-08-03  |  5KB  |  199 lines

  1.  
  2. /* 
  3. THESE ARE SOME USEFUL PROLOG PREDICATES FOR MANIPULATING LISTS AND A
  4. REAL ARITHMETIC FUNCTION OMITTED FROM TPROLOG. THE LIST DOMAIN AND PREDICATE
  5. DECLARATIONS ONLY ASSUME YOU'RE MANIPULATING SYMBOLS AND LISTS OF SYMBOLS.
  6. ADDITIONAL DOMAIN AND PREDICATE DECLARATIONS CAN MAKE THESE PREDICATES
  7. HANDLE OTHER DATA TYPES.
  8.  
  9. COMPILED BY JOHN REECE FROM C&M AND B&H
  10. */
  11.  
  12. domains
  13.  
  14.   list = symbol* 
  15.  
  16. predicates
  17.   true                               /* Standard Prolog predicate omitted from Turbo Prolog */
  18.   repeat                             /* Standard Prolog predicate omitted from Turbo Prolog */
  19.   raise(real,real,real)              /* Raise a base to a real, positive or negative exponent */
  20.  
  21.   append(list,list,list)             /* Append two lists into the third */
  22.   member(symbol,list)                /* Is symbol a member of the list? */
  23.   
  24.                                      /* Most of these need member() or append() */
  25.  
  26.   nth(symbol,integer,list)           /* Remove the nth symbol from the list */
  27.   last(symbol,list)                  /* Get the last symbol in the list */
  28.   remove(symbol,list,list)           /* Remove all occurrences of symbol from the list */
  29.   length(list,integer)               /* Number of elements in the list */
  30.   flen(list,integer,integer)         /* Subroutine for length() */
  31.   permute(list,list)                 /* Returns all the possible rearrangements of a list */
  32.   reverse(list,list)                 /* Reverse a list */
  33.   reverse1(list,list,list)           /* Subroutine for reverse() */
  34.   efface(symbol,list,list)           /* Remove the first occurence of symbol from a list*/
  35.   subst(symbol,list,symbol,list)     /* Replace all occurrences of the first symbol in the first list with the second symbol */
  36.   subset(list,list)                  /* Is the first list a subset of the second? */
  37.   intersection(list,list,list)       /* Intersection of two lists */
  38.   union(list,list,list)              /* Union of two lists */
  39.   quicksort(list,list)               /* Quick sort a list of symbols */
  40.   split(symbol,list,list,list)       /* Subroutine for quicksort() */
  41.   
  42. clauses
  43.  
  44. /*
  45. TRUE PREDICATE FROM STANDARD PROLOG
  46. */
  47. true if
  48.    not(not(fail)).
  49.  
  50. /*
  51. REPEAT PREDICATE FROM STANDARD PROLOG
  52. */
  53. repeat.
  54. repeat if repeat. 
  55.  
  56. /*
  57. RAISE A BASE TO A REAL, POSITIVE OR NEGATIVE EXPONENT, RETURNING RESULT.
  58. */
  59. raise(_,Power,Result) if
  60.     Power = 0 and
  61.     Result = 1.0.
  62.     
  63. raise(Base,Power,Result) if
  64.     Power < 0 and
  65.     Term1 = 0.0 - Power and
  66.     raise(Base,Term1,TempResult) and
  67.     Result = 1/TempResult.
  68.  
  69. raise(Base,Power,Result) if
  70.     Power > 0 and
  71.     Result = exp(Power * ln(Base)).  
  72.   
  73. /*
  74. CONCATENATE TWO LISTS INTO LIST Y.
  75. */
  76. append([],L,L).
  77. append([X|L1],L2,[X|L3]) if
  78.      append(L1,L2,L3).
  79.  
  80. /*
  81. IS X A MEMBER OF THE LIST?     
  82. */
  83. member(X, [X|_]).
  84. member(X, [_|Y]) if member(X, Y).
  85.  
  86.  
  87. /*
  88. FIND THE nTH MEMBER OF A LIST
  89. */
  90. nth(X, 1, [X|_]).
  91. nth(X, N, [_|L]) if R = N-1,
  92.                     nth(X, R, L).
  93.  
  94. /*
  95. FIND THE LAST ELEMENT X IN A LIST.
  96. */
  97. last(X,[X]).
  98. last(X,[_|L]) if last(X,L).
  99.  
  100. /* 
  101. REMOVE THE GIVEN ITEM X FROM THE LIST, RETURNING L.
  102. */
  103. remove(X,[X|T],L) if
  104.       remove(X,T,L),
  105.       !.
  106. remove(X,[X|T],T).
  107. remove(X,[H|T],[H|L]) if
  108.       remove(X,T,L).
  109. /*
  110. FIND THE LENGTH OF A LIST
  111. */
  112. length(X,J) if
  113.       flen(X,J,0).
  114.  
  115. flen([],Y,Y).
  116. flen([_|T],Y,Z) if
  117.       P = Z + 1,
  118.       flen(T, Y, P).
  119.  
  120. /*
  121. RETURN ALL PERMUTATIONS OF LIST 
  122. */
  123. permute([],[]).
  124. permute(L,[H|T]) if
  125.       append(V,[H|U],L) and
  126.       append(V,U,W) and
  127.       permute(W,T).
  128.  
  129. /*
  130. REVERSE A LIST
  131. */
  132. reverse(L1,L2) if
  133.        reverse1(L1,[],L2).
  134. reverse1([X|L],L2,L3) if
  135.        reverse1(L,[X|L2],L3).
  136. reverse1([],L,L).
  137.  
  138. /* 
  139. EFFACE(X,Y,Z) REMOVES THE FIRST OCCURENCE OF ELEMENT X FROM LIST Z
  140. */
  141. efface(A,[A|L],L) if !.
  142. efface(A,[B|L],[B|M]) if
  143.     efface(A,L,M).
  144.     
  145. /*
  146. SUBST(X,L,A,M) CONSTRUCTS NEW LIST M FROM LIST L REPLACING ALL ELEMENTS
  147. X WITH ELEMENT A
  148. */
  149. subst(_,[],_,[]).
  150. subst(X,[X|L],A,[A|M]) if
  151.       ! and
  152.       subst(X,L,A,M).
  153. subst(X,[Y|L],A,[Y|M]) if 
  154.       subst(X,L,A,M).
  155.       
  156.        
  157. /* 
  158. IS THE FIRST PARAMETER A SUBSET OF THE SECOND?
  159. */
  160. subset([A|X],Y) if
  161.        member(A,Y) and
  162.        subset(X,Y).
  163. subset([],_).
  164.  
  165. /*
  166. INTERSECTION(X,Y,Z) SUCCEEDS IF THE INTERSECTION OF X AND Y IS Z
  167. */
  168. intersection([],_,[]).
  169. intersection([X|R],Y,[X|Z]) if
  170.        member(X,Y) and
  171.        ! and
  172.        intersection(R,Y,Z).
  173. intersection([_|R],Y,Z) if
  174.        intersection(R,Y,Z).
  175.        
  176. /*
  177. UNION(X,Y,Z) SUCCEEDS IF THE UNION OF X AND Y IS Z
  178. */
  179. union([],X,X).
  180. union([X|R],Y,Z) if
  181.     member(X,Y) and
  182.     ! and
  183.     union(R,Y,Z).
  184. union([X|R],Y,[X|Z]) if
  185.     union(R,Y,Z).
  186. /*
  187. SORT A LIST OF SYMBOLS
  188. */
  189. quicksort([],[]).
  190. quicksort([H|T],S) if
  191.        split(H,T,A,B) and
  192.        quicksort(A,A1) and
  193.        quicksort(B,B1) and
  194.        append(A1,[H|B1],S).
  195. split(H,[A|X],[A|Y],Z) if A <= H and split(H,X,Y,Z).
  196. split(H,[A|X],Y,[A|Z]) if A > H and split(H,X,Y,Z).
  197. split(_,[],[],[]).
  198.  
  199.